// 导入db模块
const db = require('../config/db');

// 收藏类
class Collect {
    // 收藏领养宠物
    add(pet_name, pet_id, collect_create_time, user_id,classify_text,pet_src,pet_age){
        return new Promise((resolve,reject)=>{
            let sql2 = `select user_id from db_collect where pet_id = '${pet_id}' and user_id = '${user_id}'`;
            db.query(sql2)
            .then(
                (rows)=>{
                    if(rows.length == 0) {
                        let sql = `insert into db_collect(pet_name, pet_id, collect_create_time, user_id,classify_text,pet_src,pet_age) 
                        values('${pet_name}','${pet_id}','${collect_create_time}','${user_id}','${classify_text}','${pet_src}','${pet_age}')`;

                        db.query(sql)
                        .then(
                            (result)=>{
                                // console.log(result)
                                resolve({
                                    code: 200,
                                    msg: '收藏领养宠物成功。'
                                })
                            },
                            (err)=>{
                                reject({
                                    code: -1,
                                    msg: '收藏领养宠物失败。',
                                    err 
                                })
                            }
                        )
                    }
                    else {
                        resolve({
                            code: 1,
                            msg: '该领养宠物已经收藏了。'
                        })
                    }
                },
                (err2)=>{
                    reject({
                        code: '查询收藏领养宠物失败。'
                    })
                }
            )
        })
    }

    // 删除收藏的领养宠物
    remove(pet_id,user_id){
        return new Promise((resolve,reject)=>{
            let sql = `delete from db_collect where pet_id = '${pet_id}' and user_id = '${user_id}'`
            db.query(sql)
            .then(
                (result)=>{
                    // console.log(result);
                    resolve({
                        code: 200,
                        msg: '删除收藏的领养宠物成功。'
                    })
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '删除收藏的领养宠物失败2222。',
                        err 
                    })
                }
            )
        })
    }

    // 获取收藏的领养宠物立碑
    list(user_id){
        return new Promise((resolve,reject)=>{
            let sql = `select * from db_collect where user_id='${user_id}'`;
            db.query(sql)
            .then(
                (rows)=>{
                    resolve({
                        code: 200,
                        msg: '获取收藏领养宠物列表成功。',
                        result: rows 
                    })
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '获取收藏的领养宠物失败。',
                        err
                    })
                }
            )
        })
    }

    // 获取所有收藏数量
    total() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_collect`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计收藏数量成功。`,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 判断是否收藏的领养宠物
    is_collect(pet_id,user_id){
        return new Promise((resolve,reject)=>{
            let sql = `select user_id from db_collect where pet_id = '${pet_id}' and user_id = '${user_id}'`;
            db.query(sql)
            .then(
                (result)=>{
                    // console.log(result);
                    if(result.length == 1) {
                        resolve({
                            code: 200,
                            isCollect: true,
                            text:"是",
                            msg: '已收藏该宠物领养记录'
                        })
                    }
                    else {
                        resolve({
                            code: 200,
                            isCollect: false,
                            text:"否",
                            msg: '取消收藏该宠物领养记录'
                        })
                    }
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '查询失败',
                        err 
                    })
                }
            )
        })
    }

}

module.exports = new Collect();